package com.example.book.controller;

import com.example.book.Service.BookService;
import com.example.book.Service.UserService;
import com.example.book.constants.Constants;
import com.example.book.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    BookService bookService;
    @Autowired
    UserService userService;
    @RequestMapping(value = "/login", produces = "application/json")
    public String login(String userName, String password, HttpSession session) {
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return "用户名或密码为空";
        }
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo == null) {
            return "用户名不存在";
        }
        if(!userInfo.getPassword().equals(password)) {
            return "密码错误";
        }
        session.setAttribute(Constants.USER_SESSION_KEY, userInfo);
        return "";
    }
}
